package com.qiantu.api.db;

import android.content.Context;
import android.text.TextUtils;

import com.qiantu.api.db.database.LocalDataBase;
import com.qiantu.api.db.greendao.SourceHomepageBeanDao;
import com.qiantu.api.entity.SourceHomepageBean;

import java.util.List;

public class LLSourceHomepageDBManager {
    private final String TAG = "LLSourceHomepageDBManager";
    private volatile static LLSourceHomepageDBManager instance = null;
    private final static Object object = new Object();
    private final SourceHomepageBeanDao mSourceHomepageBeanDao;
    private Context context;

    private LLSourceHomepageDBManager(Context context) {
        this.context = context;
        mSourceHomepageBeanDao = LocalDataBase.getInstance().getDaoSession(context).getSourceHomepageBeanDao();
    }

    public static LLSourceHomepageDBManager getInstance(Context context) {
        if (instance == null) {
            synchronized (object) {
                if (instance == null) {
                    instance = new LLSourceHomepageDBManager(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public void clearFromDB() {
        mSourceHomepageBeanDao.deleteAll();
    }

    /**
     * 从服务器更新设备数据
     *
     * @param SourceHomepageBeans
     */
    public void refreshAllSoucecesTx(List<SourceHomepageBean> SourceHomepageBeans, HandleListener listener) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            clearFromDB();
            if(SourceHomepageBeans == null){
                return;
            }
            for (SourceHomepageBean SourceHomepageBean : SourceHomepageBeans) {
                mSourceHomepageBeanDao.save(SourceHomepageBean);
            }
        });
    }
    /**
     * 保存或者更新群组设备
     *
     * @param SourceHomepageBeans
     */
    public void addBatch(List<SourceHomepageBean> SourceHomepageBeans) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            for (SourceHomepageBean SourceHomepageBean : SourceHomepageBeans) {
                mSourceHomepageBeanDao.save(SourceHomepageBean);
            }
        });
    }

    public void put(SourceHomepageBean homepageBean) {
        if (homepageBean == null)
            return;
        String serialNo = homepageBean.getSerialNo();
        if (serialNo != null && !TextUtils.isEmpty(serialNo)) {
            SourceHomepageBean oldDevice = mSourceHomepageBeanDao.queryBuilder()
                    .where(SourceHomepageBeanDao.Properties.SerialNo.eq(serialNo))
                    .unique();
            if (oldDevice != null) {
                homepageBean.setLocalId(oldDevice.getLocalId());
            }
        }
        mSourceHomepageBeanDao.save(homepageBean);
    }

    public List<SourceHomepageBean> getDataByRoomSerialNo(String roomSerialNo) {
        return mSourceHomepageBeanDao.queryBuilder()
                .where(SourceHomepageBeanDao.Properties.RoomSerialNo.in(roomSerialNo))
                .list();
    }

    public void changeIsHomepage(String serialNo,boolean isHomepage) {
        if (!TextUtils.isEmpty(serialNo)) {
            SourceHomepageBean homepage = mSourceHomepageBeanDao.queryBuilder()
                    .where(SourceHomepageBeanDao.Properties.SerialNo.eq(serialNo))
                    .unique();
            if (homepage != null) {
                homepage.setIsHomePage(String.valueOf(isHomepage));
                mSourceHomepageBeanDao.save(homepage);
            }
        }
    }

    public void changeFromDevice(String serialNo,String deviceSerialNo) {
        if (!TextUtils.isEmpty(serialNo)) {
            SourceHomepageBean homepage = mSourceHomepageBeanDao.queryBuilder()
                    .where(SourceHomepageBeanDao.Properties.SerialNo.eq(serialNo))
                    .unique();
            if (homepage != null) {
                homepage.setDeviceSerialNo(deviceSerialNo);
                mSourceHomepageBeanDao.save(homepage);
            }
        }
    }
}
